package com.km.noh.weatherinfo.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class KMPreference {

	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;

	public final static String SELECTED_CITY = "select_city"; // 선택한 도시.
	public final static String SELECTED_CITY_KO = "select_city_ko"; // 선택한 도시 한글 

	public static KMPreference instance;

	private KMPreference(Context context) {
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
		editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
	}

	/**
	 * 인스턴스 생성 및 가져오기
	 * 
	 * @param context
	 * @return
	 */
	public static KMPreference getInstance(Context context) {
		if (instance == null) instance = new KMPreference(context);
		return instance;
	}

	/**
	 * 데이터삭제
	 * 
	 * @param key
	 */
	public void removeData(String key) {
		editor.remove(key);
		editor.commit();
	}

	/**
	 * 데이터 셋팅(boolean)
	 * 
	 * @param key
	 * @param val
	 * @return
	 */
	public boolean setData(String key, boolean val) {
		try {
			editor.putBoolean(key, val);
			editor.commit();

			return true;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
			return false;
		}
	}

	/**
	 * 데이터 셋팅(String)
	 * 
	 * @param info
	 * @return
	 */
	public boolean setData(String key, String val) {
		try {
			editor.putString(key, val);
			editor.commit();
			return true;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
			return false;
		}
	}

	/**
	 * 데이터 셋팅(int)
	 * 
	 * @param info
	 * @return
	 */
	public boolean setData(String key, int val) {
		try {
			editor.putInt(key, val);
			editor.commit();
			return true;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
			return false;
		}
	}
	
	public boolean setData(String key, long val) {
		try {
			editor.putLong(key, val);
			editor.commit();
			return true;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
			return false;
		}
	}

	/**
	 * 데이터 가져오기(int)
	 * 
	 * @param key
	 * @param val
	 * @return
	 */
	public int getData(String key, int defaultVal) {
		return prefs.getInt(key, defaultVal);
	}
	
	public long getData(String key, long defaultVal) {
		return prefs.getLong(key, defaultVal);
	}

	/**
	 * 데이터 가져오기(boolean)
	 * 
	 * @param key
	 * @param val
	 * @return
	 */
	public boolean getData(String key, boolean defaultVal) {
		return prefs.getBoolean(key, defaultVal);
	}

	/**
	 * 데이터 가져오기(String)
	 * 
	 * @param key
	 * @param val
	 * @return
	 */
	public String getData(String key, String defaultVal) {
		return prefs.getString(key, defaultVal);
	}

	public void clearData() {
		try {
			editor.clear();
			editor.commit();
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}
		return;
	}
}
